package com.woody.aggregate.domain.contract;

import cn.hutool.core.collection.CollUtil;
import cn.hutool.extra.spring.SpringUtil;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.woody.aggregate.contract.dto.ContractTemplatePageDTO;
import com.woody.aggregate.contract.vo.ContractTemplateVO;
import com.woody.aggregate.infrastructure.contract.entity.ContractTemplateEntity;
import com.woody.aggregate.infrastructure.contract.mapper.ContractTemplateMapper;
import com.woody.common.api.enums.ContractType;
import com.woody.common.web.util.JacksonUtil;
import org.springframework.util.StringUtils;

import java.util.List;
import java.util.Map;
import java.util.Objects;
import java.util.stream.Collectors;

/**
 * @author hzy
 * @date 2025/2/21 下午5:27
 */
public class ContractTemplate extends ContractTemplateEntity {

    public static ContractTemplateMapper mapper;

    static {
        mapper = SpringUtil.getBean(ContractTemplateMapper.class);
    }



    public void update(){
        ContractTemplateEntity entity = JacksonUtil.reGroup(this, ContractTemplateEntity.class);
        mapper.updateById(entity);
    }

    public void create(){
        ContractTemplateEntity entity = JacksonUtil.reGroup(this, ContractTemplateEntity.class);
        mapper.insert(entity);
    }


    public static Page<ContractTemplateVO> pageList(ContractTemplatePageDTO dto){
        Page<ContractTemplateVO> result = new Page<>(dto.getCurrent(),dto.getSize());

        Page<ContractTemplateEntity> page =mapper.selectPage(Page.of(dto.getCurrent(), dto.getSize()), Wrappers.lambdaQuery(ContractTemplateEntity.class)
                .orderByDesc(ContractTemplateEntity::getCreateTime));

        List<ContractTemplateVO> list = page.getRecords().stream().map(item->buildContractTemplateVO(item)).collect(Collectors.toList());
        result.setRecords(list);
        result.setTotal(page.getTotal());
        result.setPages(page.getPages());
        return result;
    }

    public static ContractTemplateVO buildContractTemplateVO(ContractTemplateEntity entity){
        ContractTemplateVO vo = new ContractTemplateVO();
        vo.setTemplateId(entity.getTemplateId());
        vo.setContractName(entity.getContractName());
        vo.setVersion("V"+entity.getVersion());
        String type = entity.getType();
        ContractType contractType = ContractType.valueOf(type);
        vo.setType(contractType.getValue());
        return vo;
    }



    public static Map<String, ContractTemplate> getNewVersionTemplate(List<String> types){
        List<ContractTemplateEntity> entityList = mapper.selectList(Wrappers.lambdaQuery(ContractTemplateEntity.class)
                .in(ContractTemplateEntity::getType, types));

        if (CollUtil.isEmpty(entityList)){
            return null;
        }

        List<ContractTemplate> list = JacksonUtil.reGroupArray(entityList, ContractTemplate[].class);

        // 按 type 分组，每个组中取 version 最大的一个
        Map<String, ContractTemplate> map = list.stream()
                .collect(Collectors.toMap(
                        ContractTemplate::getType,
                        item -> item,
                        (item1, item2) -> item1.getVersion() >= item2.getVersion() ? item1 : item2
                ));
        return map;
    }
}
